package com.codi.api.interceptor;

import com.codi.api.security.UserUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.constant.HttpHeaderConstant;
import com.codi.bus.core.cache.UserInfoCache;
import com.codi.bus.core.dao.OrganProfileDao;
import com.codi.bus.core.domain.OrganProfile;
import com.codi.bus.core.resultModel.ValidateTokenResult;
import com.codi.bus.core.service.UserService;
import com.codi.bus.exception.BaseException;
import com.codi.message.util.MessageUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Shangdu Lin on 2017/2/24 10:40.
 */
public class ValidateIsOrgInterceptor implements HandlerInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(ValidateIsOrgInterceptor.class);

    @Autowired
    private UserService userService;

    @Autowired
    private OrganProfileDao organProfileDao;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        String profileId = httpServletRequest.getHeader(GlobalConstant.REQUEST_PROFILE_ID);

        if (StringUtils.isNotBlank(profileId)){
            UserInfoCache.setIsOrg(true);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        UserUtil.clear();
        String str = UserUtil.clearOrg();
        logger.info(str);
    }
}
